package com.sdp.wx.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.cache.clean.WxCleanCache;
import com.sdp.wx.db.wx_wz_type.entity.WxWzType;
import com.sdp.wx.db.wx_wz_type.service.IWxWzTypeService;
import com.sdp.wx.utils.WxConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-17
 */
@RestController
@RequestMapping("/WxWzTypeController")
public class WxWzTypeController {

	@Autowired
	private IWxWzTypeService iWxWzTypeService;
	
	@Autowired
	private WxCleanCache wxCleanCache;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@PostMapping(value = "/queryWzTypeTableTree")
	public Result<?> queryWzTypeTableTree() {
		return Result.ok(iWxWzTypeService.queryWzTypeTableTree());
	}

	@PostMapping(value = "/queryWzTypeTreeSelect")
	public Result<?> queryWzTypeTreeSelect() {
		return Result.ok(iWxWzTypeService.queryWzTypeTreeSelect());
	}
	
	@PostMapping(value = "/save")
	public Result<?> save(@RequestBody WxWzType wxWzType) {
		if(wxWzType==null) {
			return Result.error("器材类型信息为空");
		}
		
		if(StringUtils.isEmpty(wxWzType.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxWzTypeService, WxWzType.class, new String[] {"typeNo"}, "keyId", wxWzType, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("器材类型编号不能重复");
			}
			r = setLevel(wxWzType);
			if(r==null || !r.isSuccess()) {
				return r;
			}
			if(StringUtils.isEmpty(wxWzType.getTypeOrder())) {
				wxWzType.setTypeOrder(1);
			}
			wxWzType.setIsUse(WxConstants.是.getCode());
			wxWzType.setCreateTime(LocalDateTime.now());
			iWxWzTypeService.save(wxWzType);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxWzTypeService, WxWzType.class, new String[] {"typeNo"}, "keyId", null, wxWzType, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("器材类型编号不能重复");
			}
			r = setLevel(wxWzType);
			if(r==null || !r.isSuccess()) {
				return r;
			}
			iWxWzTypeService.updateById(wxWzType);
		}
		wxCleanCache.cleanWzTypeCahce();
		return Result.ok();
	}
	
	
	
	/**
	 * 设置层级
	 * @param sysOrg
	 * @return
	 */
	private Result<?> setLevel(WxWzType wxWzType) {
		if(StringUtils.isEmpty(wxWzType.getParentNo())) {
			wxWzType.setTypeLevel("["+wxWzType.getTypeNo()+"]");
		}else if(wxWzType.getParentNo().equals(wxWzType.getTypeNo())){
			return Result.error("选择的父类型不能为自己");
		}else{
			WxWzType parentType = iWxWzTypeService.queryWzTypeByNo(wxWzType.getParentNo());
			if(parentType==null) {
				return Result.error("选择的父类型已经不存在");
			}else if(parentType.getTypeLevel().indexOf("["+wxWzType.getTypeNo()+"]")>-1) {
				return Result.error("选择的父类型不符合要求，存在相互依赖关系");
			}
			wxWzType.setTypeLevel(parentType.getTypeLevel()+"["+wxWzType.getTypeNo()+"]");

		}
		return Result.ok();
	}
}

